package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.DishFlavor;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.result.PageResult;
import com.sky.service.DishService;
import com.sky.vo.DishVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class DishServiceImpl implements DishService {

    @Autowired
    private DishMapper dishMapper;

    public PageResult listDish(DishPageQueryDTO dishPageQueryDTO){
        PageHelper.startPage(dishPageQueryDTO.getPage(), dishPageQueryDTO.getPageSize());
        Page<DishVO> pg = dishMapper.select(dishPageQueryDTO);
        return new PageResult( pg.getTotal(), pg.getResult());
    }

    public DishVO getDishById(Long id){
        DishVO dv = dishMapper.selectById(id);
        List<DishFlavor> flavors = dishMapper.getFlavorsByid(id);
        dv.setFlavors(flavors);
        return dv;
    }

    public List<Dish> getDishByCategoryId(Long categoryId){
        return dishMapper.getDishByCategoryId(categoryId);
    }


    public void updateStates(Long id, Integer status){
        Dish dish = new Dish();
        dish.setId(id);
        dish.setStatus(status);
        dishMapper.update(dish);
    }

    public void insert(DishDTO dishDTO){
        Dish dish1 = new Dish();
        BeanUtils.copyProperties(dishDTO, dish1);
        dishMapper.insert(dish1);
        dishDTO.getFlavors().forEach(flavor -> {flavor.setDishId(
                dishMapper.getIdByName(dish1.getName()));   });
        dishDTO.getFlavors().forEach(flavor -> {dishMapper.insertFlavor(flavor);});
    }

    public void update(DishDTO dishDTO){
        Dish dish1 = new Dish();
        BeanUtils.copyProperties(dishDTO, dish1);
        dishMapper.update(dish1);//更新dish表格
        dishDTO.getFlavors().forEach(flavor -> {dishMapper.deleteFlavor(flavor);});
        dishDTO.getFlavors().forEach(flavor -> {flavor.setDishId(
                dishMapper.getIdByName(dish1.getName()));   });
        dishDTO.getFlavors().forEach(flavor -> {dishMapper.insertFlavor(flavor);});
    }

    @Transactional
    public void delete(List<Long> ids){
        DishFlavor df = new DishFlavor();
        ids.forEach(id -> {
            //TODO
//            if(setMealDish.getMealIdByDishId(id) == 0){ throw new
//                    DeletionNotAllowedException(MessageConstant.DISH_ON_SALE); };

            df.setDishId(id);
            dishMapper.deleteFlavor(df);
            dishMapper.delete(id);
        });

    }

    /**
     * 条件查询菜品和口味
     * @param dish
     * @return
     */
    public List<DishVO> listWithFlavor(Dish dish) {
        List<Dish> dishList = dishMapper.getDishByCategoryId(dish.getCategoryId());

        List<DishVO> dishVOList = new ArrayList<>();

        for (Dish d : dishList) {
            DishVO dishVO = new DishVO();
            BeanUtils.copyProperties(d,dishVO);

            //根据菜品id查询对应的口味
            List<DishFlavor> flavors = dishMapper.getFlavorsByDishId(d.getId());

            dishVO.setFlavors(flavors);
            dishVOList.add(dishVO);
        }

        return dishVOList;
    }
}
